# Copyright (c) 2008-2009-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::Accounts;
use strict;
use utf8;
use DBI;
use Error::Simple;
use Account;

sub new 
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{ConnectString} = shift || throw Error::Simple("Connection string expected");
	$self->{DB} = DBI->connect("DBI:".$self->{ConnectString}, "", "", {'RaiseError' => 1, 'PrintError' => 0, 'unicode' => 1});
	bless ($self, $class);
	$self->load();
	return $self;
}

sub load
{
	my $self = shift;
	$self->{Accounts} = {};
	eval 
	{ 
#		printf STDERR "Loading accounts\n";
		my $stq = $self->{DB}->prepare("select * from account left join player_name where account.id = player_name.player and account.id = player_name.reporter_id and account.active = 1");
		printf STDERR "Error querying accounts\n" unless $stq->execute();
		while (my $acc = $stq->fetchrow_hashref())
		{
			$self->{Accounts}->{$acc->{'name'}} = dcExplorer::Account->new(
				$acc->{'id'}, $acc->{'username'}, $acc->{'password'},	$acc->{'x'}, $acc->{'y'}
			);
#			printf STDERR "Loaded account: %s\n", $acc->{'username'};
		}
		$stq->finish();
	};
}

sub names
{
	my $self = shift;
	return sort keys %{$self->{Accounts}};
}

sub account
{
	my $self = shift;
	my $name = shift;
	return $self->{Accounts}->{$name};
}

sub Add
{
	my $self = shift;
	my $username = shift;
	my $password = shift;
	my $x = shift;
	my $y = shift;
	my $session = dcExplorer::server::Session->new($username, $password);
	my $xml = $session->GetXML('general');
	if ($xml)
	{
		my $empire = dcExplorer::core::Empire->new;
		my $parser = dcExplorer::loader::OriginalXML->new($empire);
		$parser->loadXML($xml);
		my ($id, $user) = ($empire->getProp('player_id'), $empire->getProp('name'));
		$x = $x || $empire->getProp('homeworldx');
		$y = $y || $empire->getProp('homeworldy');
		if (defined($id) && defined($user) && ($id =~ /^\d+$/))
		{
			my $dbh;
			$dbh = $self->{DB}->prepare("insert into account(id, username, password, x, y) values (?, ?, ?, ?, ?)");
			$dbh->execute($id, $username, $password, $x, $y);
			$dbh = $self->{DB}->prepare("insert into player_name(player, reporter_id, turn, name) values (?, ?, ?, ?)");
			$dbh->execute($id, $id, 0, $user);
			$self->{Accounts}->{$user} = dcExplorer::Account->new($id, $username, $password, $x, $y);
			return $user;
		}
	}
	throw Error::Simple("Usage is limited to registered and paid accounts only!");
}

1;
